package com.sohoometer.assignmeter.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.sohoometer.assignmeter.bean.AssignMeterDataExceptionQuery;

@Repository("assignMeterDataExceptionQueryDao")
public class AssignMeterDataExceptionQueryDaoImpl  extends GenericDaoImpl<AssignMeterDataExceptionQuery, String> implements AssignMeterDataExceptionQueryDao{
	@Autowired
	private JdbcTemplate  jdbcTemplate = null;
	@Autowired
public AssignMeterDataExceptionQueryDaoImpl(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
}
	
	
	public  ResultWithPageCond<AssignMeterDataExceptionQuery> queryList(AssignMeterDataExceptionQuery query,PageCond pageCond) throws Exception {
		 ResultWithPageCond<AssignMeterDataExceptionQuery> result = queryEntityListEqualTWithPageExt(query, pageCond);
		 return result;
	}
	
	@Override
	public ResultWithPageCond<AssignMeterDataExceptionQuery> queryEntityListEqualTWithPageExt(
			AssignMeterDataExceptionQuery entity, PageCond pageCond)
			throws Exception {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(AssignMeterDataExceptionQuery.class);
	     if (entity != null) {
	       Example exam = Example.create(entity);
	       exam.excludeZeroes();
	       detachedCriteria.add(exam);
	     }
	     
	     List<String[]> orderList = pageCond.getOrderList();
	     if ((orderList != null) && (orderList.size() > 0)) {
	       for (String[] order : orderList) {
	         String orderField = order[0];
	         String orderBy = order[1];
	         if ("desc".equals(orderBy)) {
	           detachedCriteria.addOrder(Order.desc(orderField));
	         } else {
	           detachedCriteria.addOrder(Order.asc(orderField));
	         }
	       }
	     }
	     List<AssignMeterDataExceptionQuery> list = getHibernateTemplate().findByCriteria(detachedCriteria, 
	       pageCond.getStart(), pageCond.getLimit());
	     int total = getRowCount(detachedCriteria);
	     pageCond.setTotal(total);
	     ResultWithPageCond<AssignMeterDataExceptionQuery> rwpc = new ResultWithPageCond();
	     rwpc.setList(list);
	     rwpc.setPageCond(pageCond);
	     return rwpc;
	}
	

	
	
	@Override
	protected int getRowCount(DetachedCriteria criteria) throws Exception {
		criteria.setProjection(Projections.rowCount());
	   List list = queryEntityListCriteria(criteria, 0, 1);
	    return ((Long)list.get(0)).intValue();
	}

	
}
